const baseUrl = 'http://localhost:7001/public'
const renderHtml = (dataArr) => {
  return `<!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>vue-test</title>
      </head>
      <body>
       
        ${getModuleHtml(dataArr)}
      </body>
    </html>
    `
}
const getModuleHtml = (arr) => {
  let strScript = ''
 
  arr.forEach((v,i) => {
    strScript += `<div id="module${i + 1}"></div>`;
    strScript +=`<script src="${baseUrl}/${v.module_name}/${v.edition}/daily/index.js"></script>`;
  })
  
  const strArr = arr.map(v => v.module_name);
  const propsArr = arr.map((v,i) => (JSON.stringify({
    ...v.props,
    module_id: `module` + (i+1)
  })));
  console.log(arr)
  strScript += `<script defer>
        let moduleArr= ${JSON.stringify(strArr)}; 
        let propsArr= ${JSON.stringify(propsArr)}; 
        for(var i = 0; i < moduleArr.length; i++){
          window[moduleArr[i]](JSON.parse(propsArr[i])).$mount("#module"+(i+1));
        }
    </script>`
  return strScript
}

/*
*判断是否是一个对象
*/
const isObject=(obj)=>{
  return  (typeof obj==="object")&&obj!==null;
}
/*
 *深copy函数，用以树形结构的当前子节点的copy 
 */
const deepCopy = (obj, hash = new WeakMap()) => {
  if(!isObject(obj)){
    throw Error("不是一个对象")
  }
  if (hash.has(obj)) return hash.get(obj);
  let newObj=Array.isArray(obj)?[]:{};
  hash.set(obj,newObj);
  Object.keys(obj).forEach(v=>{
    if(isObject(obj[v])){
      newObj[v]=deepCopy(obj[v],hash)
    }else{
      newObj[v]=obj[v];
    }
  })
  return newObj
}
const upData = (arr, index) => {
  console.log(index)
  if(index === 0) {
    return
  }
  console.log(1)
  let list = deepCopy(arr);
  //在上一项插入该项
  list[index] = list.splice(index - 1, 1, (list[index]))[0]
  return list;
}

const downData = (arr, index) => {
  if(index === arr.length - 1) {
    return
  }
  let list = deepCopy(arr);
  //在上一项插入该项
  list[index] = list.splice(index + 1, 1, (list[index]))[0]
  return list;
}
export default {
  renderHtml,
  deepCopy,
  upData,
  downData
}
